import { apiClient } from './config'
import type {
  ApiResponse,
  LoginUserVO,
  UserLoginRequest,
  UserRegisterRequest,
  UserEditRequest,
  UserAddRequest,
  UserUpdateRequest,
  UserQueryRequest,
  DeleteRequest,
  UserVO,
  User,
  PageResponse,
} from '@/types/user'

// 用户API服务
export const userApi = {
  // 用户登录
  login: (data: UserLoginRequest): Promise<ApiResponse<LoginUserVO>> => {
    return apiClient.post('/user/login', data)
  },

  // 用户注册
  register: (data: UserRegisterRequest): Promise<ApiResponse<string>> => {
    return apiClient.post('/user/register', data)
  },

  // 用户注销
  logout: (): Promise<ApiResponse<boolean>> => {
    return apiClient.post('/user/logout')
  },

  // 获取当前登录用户
  getLoginUser: (): Promise<ApiResponse<LoginUserVO>> => {
    return apiClient.get('/user/get/login')
  },

  // 编辑用户（用户自己编辑）
  editUser: (data: UserEditRequest): Promise<ApiResponse<boolean>> => {
    return apiClient.post('/user/edit', data)
  },

  // 管理员添加用户
  addUser: (data: UserAddRequest): Promise<ApiResponse<string>> => {
    return apiClient.post('/user/add', data)
  },

  // 管理员更新用户
  updateUser: (data: UserUpdateRequest): Promise<ApiResponse<boolean>> => {
    return apiClient.post('/user/update', data)
  },

  // 管理员删除用户
  deleteUser: (data: DeleteRequest): Promise<ApiResponse<boolean>> => {
    return apiClient.delete('/user/delete', { data })
  },

  // 管理员根据ID查询用户
  getUserById: (id: string): Promise<ApiResponse<User>> => {
    return apiClient.get('/user/get', { params: { id } })
  },

  // 普通用户根据ID查询用户
  getUserVOById: (id: string): Promise<ApiResponse<UserVO>> => {
    return apiClient.get('/user/get/vo', { params: { id } })
  },

  // 管理员分页查询用户
  listUserVOByPage: (data: UserQueryRequest): Promise<ApiResponse<PageResponse<UserVO>>> => {
    return apiClient.post('/user/list/page/vo', data)
  },

  // 健康检查
  health: (): Promise<ApiResponse<string>> => {
    return apiClient.get('/test/health')
  },
}
